#include "widget.h"
#include "ui_widget.h"
#include "myini.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //检查config.ini是否创建
    MyIni::ensureConfigFile();

    //管理类初始化
    CameraManager *groundLongManager = new CameraManager(_groundLongWV);
    m_managers.append(groundLongManager);

    groundLongManager->init(); //地面长波初始化

    CameraManager *groundMidManager = new CameraManager(_groundMidWV);
    m_managers.append(groundMidManager);

    groundMidManager->init();  //地面中波初始化

//    CameraManager *airVisibleManager = new CameraManager(_airVisble);
//    m_managers.append(airVisibleManager);

//    airVisibleManager->init();  //空中可见光初始化

    CameraManager *airMidWvManager = new CameraManager(_airMidWV);
    m_managers.append(airMidWvManager);

    airMidWvManager->init();  //空中中波红外初始化

    groundLongManager->startEncode();

    groundMidManager->startEncode();

//    airVisibleManager->startEncode();

    airMidWvManager->startEncode();

    for(int i = 0; i < m_managers.size(); i++) {
        //连接错误输出
        connect(m_managers[i], &CameraManager::sigErrorOccurred,
                this, &Widget::onErrorOccurred);
        //连接界面显示
        connect(m_managers[i], &CameraManager::sigToShowUi, [this](CAMERA camera, int width, int height, int fps){
            switch (camera) {
            case _airMidWV: {
                ui->lineEdit_20->setText(QString("%1 * %2").arg(QString::number(width)).arg(QString::number(height)));
                ui->lineEdit_21->setText(QString::number(fps));
                ui->lineEdit_19->setText("h264");
                break;
            }
            case _airVisble: {
                break;
            }
            case _groundLongWV: {
                ui->lineEdit->setText(QString("%1 * %2").arg(QString::number(width)).arg(QString::number(height)));
                ui->lineEdit_2->setText(QString::number(fps));
                ui->lineEdit_3->setText("h264");
                break;
            }
            case _groundMidWV: {
                ui->lineEdit_17->setText(QString("%1 * %2").arg(QString::number(width)).arg(QString::number(height)));
                ui->lineEdit_18->setText(QString::number(fps));
                ui->lineEdit_19->setText("h264");
                break;
            }
            default:
                break;
            }
        });
    }
}

Widget::~Widget()
{
    for(int i = 0; i < m_managers.size(); i++) {
        m_managers[i]->stopEncode();
        delete m_managers[i];
    }
}

void Widget::onErrorOccurred(const QString &msg)
{
    qWarning() << "出现错误:" << msg;
}

void Widget::onUpdateUi()
{

}

